//
//  HttpRequestLoading.m
//  Autobiography
//
//  Created by lwx on 17/3/30.
//  Copyright © 2017年 Lwx. All rights reserved.
//

#import "HttpRequestLoading.h"

static HttpRequestLoading *currentUser;
static NSMutableArray *photoTasks;
static NSMutableArray *vadioTasks;

static NSMutableArray *isLoadingArr;
static NSMutableArray *noLoadingArr;/** 0 ~ 5*/

@interface HttpRequestLoading()

@end

@implementation HttpRequestLoading

+ (instancetype)intance
{
    if(currentUser == nil)
    {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            currentUser = [[HttpRequestLoading alloc] init];
        });
    }
    return currentUser;
}

//照片请求数组
- (NSMutableArray *)allPhotoTasks
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (photoTasks == nil)
        {
            photoTasks = [[NSMutableArray alloc] init];
        }
    });
    return photoTasks;
}

//视频请求数组
- (NSMutableArray *)allVadioTasks
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (vadioTasks == nil)
        {
            vadioTasks = [[NSMutableArray alloc] init];
        }
    });
    return vadioTasks;
}

//正在上传中队列序号
- (NSMutableArray *)isLoadingTasks
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (isLoadingArr == nil)
        {
            isLoadingArr = [[NSMutableArray alloc] init];
        }
    });
    return isLoadingArr;
}

- (NSMutableArray *)noLoadingTasks
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (noLoadingArr == nil)
        {
            noLoadingArr = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
        }
    });
    return noLoadingArr;
}

- (void)upLodingPhoto:(NSArray *)array uid:(NSString *)uid
{
    if ([self isFillTasks]){return;}
    NSString *location = [self addLoadingArr];
    
    WS(ws);
    HttpLoadingModel *model = [[HttpLoadingModel alloc]init];
    model.image = array[0];
    model.location = location;
    model.session =[[HttpRequest shardWebUtil]postNetworkRequestURLString:Url(@"autobiography/picture/savePicture.do") parameters:@{@"uid":uid} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        for (UIImage *image in array)
        {
            NSData *data = UIImageJPEGRepresentation(image, 0.5);
            NSString *imgName = [NSString stringWithFormat:@"%ld%ld.jpg",(NSUInteger)[NSDate date].timeIntervalSince1970,data.length];
            [formData appendPartWithFileData:data name:@"files" fileName:imgName mimeType:@"image/jpeg"];
        }
        
    } progress:^(NSProgress *uploadProgress) {
        
        model.progress = [NSString stringWithFormat:@"%.02f",uploadProgress.fractionCompleted];
//        NSDictionary *dict = @{@"progress":uploadProgress,@"location":location};
//        NSNotification *notification =[NSNotification notificationWithName:@"UpLodingPhotoNotification" object:nil userInfo:dict];
//        [[NSNotificationCenter defaultCenter] postNotification:notification];
        
    } success:^(id obj) {
        [[ws allPhotoTasks] removeObject:model];
        [ws removeLoadingArr:location];
        NSDictionary *dict = @{@"finish":@(1)};
        NSNotification *notification =[NSNotification notificationWithName:@"UpLodingNotification" object:nil userInfo:dict];
        [[NSNotificationCenter defaultCenter] postNotification:notification];
        
    } fail:^(NSError *error) {
        [[ws allPhotoTasks] removeObject:model];
        [ws removeLoadingArr:location];
        NSDictionary *dict = @{@"finish":@(0)};
        NSNotification *notification =[NSNotification notificationWithName:@"UpLodingNotification" object:nil userInfo:dict];
        [[NSNotificationCenter defaultCenter] postNotification:notification];
    }];
    [[self allPhotoTasks]addObject:model];
    NSDictionary *dict = @{@"finish":@(0)};
    NSNotification *notification =[NSNotification notificationWithName:@"UpLodingNotification" object:nil userInfo:dict];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
}

- (void)upLodingVadio:(NSData *)file model:(HttpLoadingModel *)model uid:(NSString *)uid
{
    if ([self isFillTasks]){return;}
    NSString *location = [self addLoadingArr];
    model.location = location;
    WS(ws);
    model.session =[[HttpRequest shardWebUtil]postNetworkRequestURLString:Url(@"autobiography/video/saveVideo.do") parameters:@{@"uid":uid} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        NSString *fileName = [NSString stringWithFormat:@"%ld.mp4",(NSUInteger)[NSDate date].timeIntervalSince1970];
        [formData appendPartWithFileData:file name:@"file" fileName:fileName mimeType:@"video/mp4"];
        
    } progress:^(NSProgress *uploadProgress) {
        
        model.progress = [NSString stringWithFormat:@"%.02f",uploadProgress.fractionCompleted];
//        NSDictionary *dict = @{@"progress":uploadProgress,@"location":location};
//        NSNotification *notification =[NSNotification notificationWithName:@"UpLodingVadioNotification" object:nil userInfo:dict];
//        [[NSNotificationCenter defaultCenter] postNotification:notification];
        
    } success:^(id obj) {
        [[ws allVadioTasks] removeObject:model];
        [ws removeLoadingArr:location];
        NSDictionary *dict = @{@"finish":@(1)};
        NSNotification *notification =[NSNotification notificationWithName:@"UpLodingNotification" object:nil userInfo:dict];
        [[NSNotificationCenter defaultCenter] postNotification:notification];
        
    } fail:^(NSError *error) {
        [[ws allVadioTasks] removeObject:model];
        [ws removeLoadingArr:location];
        NSDictionary *dict = @{@"finish":@(0)};
        NSNotification *notification =[NSNotification notificationWithName:@"UpLodingNotification" object:nil userInfo:dict];
        [[NSNotificationCenter defaultCenter] postNotification:notification];
    }];
    [[self allVadioTasks]addObject:model];
    NSDictionary *dict = @{@"finish":@(0)};
    NSNotification *notification =[NSNotification notificationWithName:@"UpLodingNotification" object:nil userInfo:dict];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
}

- (BOOL)isFillTasks
{
    if ([self isLoadingTasks].count == 5)
    {
        [[HUDHelper sharedInstance]tipMessage:@"上传队列已满，请等待其他任务结束再上传"];
        return YES;
    }
    return NO;
}

//添加任务是需要调用
- (NSString *)addLoadingArr
{
    @synchronized(self)
    {
        NSString *location = [self noLoadingTasks][0];
        [[self noLoadingTasks]removeObjectAtIndex:0];
        [[self isLoadingTasks]addObject:location];
        return location;
    }
}

//删除任务时需要调用
- (void)removeLoadingArr:(NSString *)location
{
    @synchronized(self)
    {
        [[self noLoadingTasks]addObject:location];
        [[self isLoadingTasks]removeObject:location];
    }
}

@end
